<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">&quot;use strict&quot;;
Object.defineProperty(exports, &quot;__esModule&quot;, { value: true });
exports.HtmlTag = void 0;
var regex_lib_1 = require(&quot;./regex-lib&quot;);
<span id='Autolinker-HtmlTag'>/**
</span> * @class Autolinker.HtmlTag
 * @extends Object
 *
 * Represents an HTML tag, which can be used to easily build/modify HTML tags programmatically.
 *
 * Autolinker uses this abstraction to create HTML tags, and then write them out as strings. You may also use
 * this class in your code, especially within a {@link Autolinker#replaceFn replaceFn}.
 *
 * ## Examples
 *
 * Example instantiation:
 *
 *     var tag = new Autolinker.HtmlTag( {
 *         tagName : &#39;a&#39;,
 *         attrs   : { &#39;href&#39;: &#39;http://google.com&#39;, &#39;class&#39;: &#39;external-link&#39; },
 *         innerHtml : &#39;Google&#39;
 *     } );
 *
 *     tag.toAnchorString();  // &lt;a href=&quot;http://google.com&quot; class=&quot;external-link&quot;&gt;Google&lt;/a&gt;
 *
 *     // Individual accessor methods
 *     tag.getTagName();                 // &#39;a&#39;
 *     tag.getAttr( &#39;href&#39; );            // &#39;http://google.com&#39;
 *     tag.hasClass( &#39;external-link&#39; );  // true
 *
 *
 * Using mutator methods (which may be used in combination with instantiation config properties):
 *
 *     var tag = new Autolinker.HtmlTag();
 *     tag.setTagName( &#39;a&#39; );
 *     tag.setAttr( &#39;href&#39;, &#39;http://google.com&#39; );
 *     tag.addClass( &#39;external-link&#39; );
 *     tag.setInnerHtml( &#39;Google&#39; );
 *
 *     tag.getTagName();                 // &#39;a&#39;
 *     tag.getAttr( &#39;href&#39; );            // &#39;http://google.com&#39;
 *     tag.hasClass( &#39;external-link&#39; );  // true
 *
 *     tag.toAnchorString();  // &lt;a href=&quot;http://google.com&quot; class=&quot;external-link&quot;&gt;Google&lt;/a&gt;
 *
 *
 * ## Example use within a {@link Autolinker#replaceFn replaceFn}
 *
 *     var html = Autolinker.link( &quot;Test google.com&quot;, {
 *         replaceFn : function( match ) {
 *             var tag = match.buildTag();  // returns an {@link Autolinker.HtmlTag} instance, configured with the Match&#39;s href and anchor text
 *             tag.setAttr( &#39;rel&#39;, &#39;nofollow&#39; );
 *
 *             return tag;
 *         }
 *     } );
 *
 *     // generated html:
 *     //   Test &lt;a href=&quot;http://google.com&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;google.com&lt;/a&gt;
 *
 *
 * ## Example use with a new tag for the replacement
 *
 *     var html = Autolinker.link( &quot;Test google.com&quot;, {
 *         replaceFn : function( match ) {
 *             var tag = new Autolinker.HtmlTag( {
 *                 tagName : &#39;button&#39;,
 *                 attrs   : { &#39;title&#39;: &#39;Load URL: &#39; + match.getAnchorHref() },
 *                 innerHtml : &#39;Load URL: &#39; + match.getAnchorText()
 *             } );
 *
 *             return tag;
 *         }
 *     } );
 *
 *     // generated html:
 *     //   Test &lt;button title=&quot;Load URL: http://google.com&quot;&gt;Load URL: google.com&lt;/button&gt;
 */
var HtmlTag =  (function () {
<span id='Autolinker-HtmlTag-method-constructor'>    /**
</span>     * @method constructor
     * @param {Object} [cfg] The configuration properties for this class, in an Object (map)
     */
    function HtmlTag(cfg) {
        if (cfg === void 0) { cfg = {}; }
<span id='Autolinker-HtmlTag-cfg-tagName'>        /**
</span>         * @cfg {String} tagName
         *
         * The tag name. Ex: &#39;a&#39;, &#39;button&#39;, etc.
         *
         * Not required at instantiation time, but should be set using {@link #setTagName} before {@link #toAnchorString}
         * is executed.
         */
        this.tagName = &#39;&#39;; // default value just to get the above doc comment in the ES5 output and documentation generator
<span id='Autolinker-HtmlTag-cfg-attrs'>        /**
</span>         * @cfg {Object.&lt;String, String&gt;} attrs
         *
         * An key/value Object (map) of attributes to create the tag with. The keys are the attribute names, and the
         * values are the attribute values.
         */
        this.attrs = {}; // default value just to get the above doc comment in the ES5 output and documentation generator
<span id='Autolinker-HtmlTag-cfg-innerHTML'>        /**
</span>         * @cfg {String} innerHTML
         *
         * The inner HTML for the tag.
         */
        this.innerHTML = &#39;&#39;; // default value just to get the above doc comment in the ES5 output and documentation generator
        this.tagName = cfg.tagName || &#39;&#39;;
        this.attrs = cfg.attrs || {};
        this.innerHTML = cfg.innerHtml || cfg.innerHTML || &#39;&#39;; // accept either the camelCased form or the fully capitalized acronym as in the DOM
    }
<span id='Autolinker-HtmlTag-method-setTagName'>    /**
</span>     * Sets the tag name that will be used to generate the tag with.
     *
     * @param {String} tagName
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setTagName = function (tagName) {
        this.tagName = tagName;
        return this;
    };
<span id='Autolinker-HtmlTag-method-getTagName'>    /**
</span>     * Retrieves the tag name.
     *
     * @return {String}
     */
    HtmlTag.prototype.getTagName = function () {
        return this.tagName || &#39;&#39;;
    };
<span id='Autolinker-HtmlTag-method-setAttr'>    /**
</span>     * Sets an attribute on the HtmlTag.
     *
     * @param {String} attrName The attribute name to set.
     * @param {String} attrValue The attribute value to set.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setAttr = function (attrName, attrValue) {
        var tagAttrs = this.getAttrs();
        tagAttrs[attrName] = attrValue;
        return this;
    };
<span id='Autolinker-HtmlTag-method-getAttr'>    /**
</span>     * Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns `undefined`.
     *
     * @param {String} attrName The attribute name to retrieve.
     * @return {String} The attribute&#39;s value, or `undefined` if it does not exist on the HtmlTag.
     */
    HtmlTag.prototype.getAttr = function (attrName) {
        return this.getAttrs()[attrName];
    };
<span id='Autolinker-HtmlTag-method-setAttrs'>    /**
</span>     * Sets one or more attributes on the HtmlTag.
     *
     * @param {Object.&lt;String, String&gt;} attrs A key/value Object (map) of the attributes to set.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setAttrs = function (attrs) {
        Object.assign(this.getAttrs(), attrs);
        return this;
    };
<span id='Autolinker-HtmlTag-method-getAttrs'>    /**
</span>     * Retrieves the attributes Object (map) for the HtmlTag.
     *
     * @return {Object.&lt;String, String&gt;} A key/value object of the attributes for the HtmlTag.
     */
    HtmlTag.prototype.getAttrs = function () {
        return this.attrs || (this.attrs = {});
    };
<span id='Autolinker-HtmlTag-method-setClass'>    /**
</span>     * Sets the provided `cssClass`, overwriting any current CSS classes on the HtmlTag.
     *
     * @param {String} cssClass One or more space-separated CSS classes to set (overwrite).
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setClass = function (cssClass) {
        return this.setAttr(&#39;class&#39;, cssClass);
    };
<span id='Autolinker-HtmlTag-method-addClass'>    /**
</span>     * Convenience method to add one or more CSS classes to the HtmlTag. Will not add duplicate CSS classes.
     *
     * @param {String} cssClass One or more space-separated CSS classes to add.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.addClass = function (cssClass) {
        var classAttr = this.getClass(), classes = !classAttr ? [] : classAttr.split(regex_lib_1.whitespaceRe), newClasses = cssClass.split(regex_lib_1.whitespaceRe), newClass;
        while ((newClass = newClasses.shift())) {
            if (classes.indexOf(newClass) === -1) {
                classes.push(newClass);
            }
        }
        this.getAttrs()[&#39;class&#39;] = classes.join(&#39; &#39;);
        return this;
    };
<span id='Autolinker-HtmlTag-method-removeClass'>    /**
</span>     * Convenience method to remove one or more CSS classes from the HtmlTag.
     *
     * @param {String} cssClass One or more space-separated CSS classes to remove.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.removeClass = function (cssClass) {
        var classAttr = this.getClass(), classes = !classAttr ? [] : classAttr.split(regex_lib_1.whitespaceRe), removeClasses = cssClass.split(regex_lib_1.whitespaceRe), removeClass;
        while (classes.length &amp;&amp; (removeClass = removeClasses.shift())) {
            var idx = classes.indexOf(removeClass);
            if (idx !== -1) {
                classes.splice(idx, 1);
            }
        }
        this.getAttrs()[&#39;class&#39;] = classes.join(&#39; &#39;);
        return this;
    };
<span id='Autolinker-HtmlTag-method-getClass'>    /**
</span>     * Convenience method to retrieve the CSS class(es) for the HtmlTag, which will each be separated by spaces when
     * there are multiple.
     *
     * @return {String}
     */
    HtmlTag.prototype.getClass = function () {
        return this.getAttrs()[&#39;class&#39;] || &#39;&#39;;
    };
<span id='Autolinker-HtmlTag-method-hasClass'>    /**
</span>     * Convenience method to check if the tag has a CSS class or not.
     *
     * @param {String} cssClass The CSS class to check for.
     * @return {Boolean} `true` if the HtmlTag has the CSS class, `false` otherwise.
     */
    HtmlTag.prototype.hasClass = function (cssClass) {
        return (&#39; &#39; + this.getClass() + &#39; &#39;).indexOf(&#39; &#39; + cssClass + &#39; &#39;) !== -1;
    };
<span id='Autolinker-HtmlTag-method-setInnerHTML'>    /**
</span>     * Sets the inner HTML for the tag.
     *
     * @param {String} html The inner HTML to set.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setInnerHTML = function (html) {
        this.innerHTML = html;
        return this;
    };
<span id='Autolinker-HtmlTag-method-setInnerHtml'>    /**
</span>     * Backwards compatibility method name.
     *
     * @param {String} html The inner HTML to set.
     * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.
     */
    HtmlTag.prototype.setInnerHtml = function (html) {
        return this.setInnerHTML(html);
    };
<span id='Autolinker-HtmlTag-method-getInnerHTML'>    /**
</span>     * Retrieves the inner HTML for the tag.
     *
     * @return {String}
     */
    HtmlTag.prototype.getInnerHTML = function () {
        return this.innerHTML || &#39;&#39;;
    };
<span id='Autolinker-HtmlTag-method-getInnerHtml'>    /**
</span>     * Backward compatibility method name.
     *
     * @return {String}
     */
    HtmlTag.prototype.getInnerHtml = function () {
        return this.getInnerHTML();
    };
<span id='Autolinker-HtmlTag-method-toAnchorString'>    /**
</span>     * Generates the HTML string for the tag.
     *
     * @return {String}
     */
    HtmlTag.prototype.toAnchorString = function () {
        var tagName = this.getTagName(), attrsStr = this.buildAttrsStr();
        attrsStr = attrsStr ? &#39; &#39; + attrsStr : &#39;&#39;; // prepend a space if there are actually attributes
        return [&#39;&lt;&#39;, tagName, attrsStr, &#39;&gt;&#39;, this.getInnerHtml(), &#39;&lt;/&#39;, tagName, &#39;&gt;&#39;].join(&#39;&#39;);
    };
<span id='Autolinker-HtmlTag-method-buildAttrsStr'>    /**
</span>     * Support method for {@link #toAnchorString}, returns the string space-separated key=&quot;value&quot; pairs, used to populate
     * the stringified HtmlTag.
     *
     * @protected
     * @return {String} Example return: `attr1=&quot;value1&quot; attr2=&quot;value2&quot;`
     */
    HtmlTag.prototype.buildAttrsStr = function () {
        if (!this.attrs)
            return &#39;&#39;; // no `attrs` Object (map) has been set, return empty string
        var attrs = this.getAttrs(), attrsArr = [];
        for (var prop in attrs) {
            if (attrs.hasOwnProperty(prop)) {
                attrsArr.push(prop + &#39;=&quot;&#39; + attrs[prop] + &#39;&quot;&#39;);
            }
        }
        return attrsArr.join(&#39; &#39;);
    };
    return HtmlTag;
}());
exports.HtmlTag = HtmlTag;
//# sourceMappingURL=html-tag.js.map</pre>
</body>
</html>
